/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library.control.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import library.book.Book;
import library.control.IControlBook;

/**
 *
 * @author MonStar
 */
public class FileControl implements IControlBook {

    private List<Book> books;
    private String route = "src\\library\\library";

    public FileControl() {
        books = new ArrayList<>();
        File file = new File(route);
        String[] list = file.list();
        if (list != null) {
            String nameBook = "";
            String autorBook = "";
            String yearBook = "";
            for (String absolutName : list) {
                for (int i = 0; i < absolutName.length(); i++) {

                    while (absolutName.charAt(i) != '.') {
                        nameBook += absolutName.charAt(i);
                        i++;
                    }
                    i++;
                    while (absolutName.charAt(i) != '.') {
                        autorBook += absolutName.charAt(i);
                        i++;
                    }
                    i++;
                    while (absolutName.charAt(i) != '.') {
                        yearBook += absolutName.charAt(i);
                        i++;
                    }
                    i++;
                    while (i < absolutName.length()) {
                        i++;

                    }
                }
                Book book = new Book();
                book.setId();
                book.setName(nameBook);
                book.setAutor(autorBook);
                book.setYear(Integer.parseInt(yearBook));
                book.setLink(file.getAbsolutePath());
                nameBook = "";
                autorBook = "";
                yearBook = "";
                books.add(book);
            }
        }
    }

    @Override
    public List<Book> getListBooks() {
        return books;
    }

    @Override
    public String getName() {
        return "file";
    }

    @Override
    public List<Book> addBook(Book book) {
        String nameNewBook = route;
        nameNewBook += "\\" + book.getName() + "." + book.getAutor() + "." + book.getYear().toString() + ".txt";
        File file = new File(nameNewBook);
        try {
            System.out.println("Файл создан = " + file.createNewFile());
        } catch (IOException ex) {
            System.out.println("Ошибка создания файла " + file.getAbsolutePath());
        }
        books.add(book);
        return books;
    }

    @Override
    public List<Book> removeBook(Book book) {
        String nameNewBook = route;
        nameNewBook += "\\" + book.getName() + "." + book.getAutor() + "." + book.getYear().toString() + ".txt";
        File file = new File(nameNewBook);
        file.delete();
        books.remove(book);
        return books;
    }
}
